#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>

void menu()
{
    //输出菜单
    printf("**************************\n");
    printf("******** [1] play ********\n");
    printf("******** [0] exit ********\n");
    printf("**************************\n");
}

void game()
{
    int guess = 0;
    int n = 1, sum = 0;
    // 0 ~ RAND_MAX(32767)
    // 生成随机数
    RAND_MAX;
    int ret = rand() % 100 + 1; // 生成随机数的函数
                    //生成1 ~ 100的数字
    while ( 1 )
    {
        Sleep(500);

        printf("请猜一个数字:");
        scanf("%d",&guess);

        Sleep(500);

        if (guess > ret)
        {
            printf("猜大了(第%d次猜)\n\n",n);
        }
        else if (guess < ret)
        {
            printf("猜小了(第%d次猜)\n\n",n);
        }
        else
        {
            printf("恭喜你，猜对了(共猜了%d次)\n\n",n-1);
            break;
        }
        
        n++;
    }
}

int main()
{
    int input = 0;
    
    srand((unsigned int)time(NULL));
    
    do
    {
        menu();
        Sleep(500);
        printf("请输入一个选择:");
        scanf("%d",&input);
        switch (input)
        {
        case 1:
            printf("开始游戏\n");
            Sleep(500);
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("无效选择，请重新选择\n");
            Sleep(500);
            break;
        }
    } while ( input );
    
    return 0;
}